package com.hinner.controller;

import com.hinner.common.Result;
import com.hinner.configuration.SftpProperties;
import com.hinner.utils.SftpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
import java.util.Vector;


@RestController
@RequestMapping("/sftp")
public class SftpController {

    @Autowired
    private SftpProperties sftpProperties;
    @Autowired
    private SftpUtils sftpUtils;


    /**
     * 直接上传到文件服务器
     * @param files
     * @return
     */
    @PostMapping
    public String upload(@RequestParam("files")MultipartFile files){
        String unique = null;
        try {
            SftpUtils sftp = new SftpUtils(sftpProperties.getSftpHost(),sftpProperties.getSftpUsername(),sftpProperties.getSftpPassword(), sftpProperties.getSftpPort(),"UTF-8");
            unique = sftp.uploadFile(files, sftpProperties.getSftpBasePath());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return  unique;
    }

    /**
     * 直接下载返回给前端
     * @return
     * @throws Exception
     */
    @PutMapping
    public void download(String flag,HttpServletResponse response) throws Exception {
//        System.out.println("======================" + flag);
        SftpUtils sftp = new SftpUtils(sftpProperties.getSftpHost(),sftpProperties.getSftpUsername(),sftpProperties.getSftpPassword(), sftpProperties.getSftpPort(),"UTF-8");
        String filename = sftp.downloadFile(flag, sftpProperties.getSftpBasePath());
        // 从文件服务器拿数据 todo

    }

}
